﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DBQLTV;

namespace QuanLyThuVien
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            LoadDataGrid_DSDocGia();
            LoadDataGrid_DSSach();

            //Chuyển ngôn ngữ
            MessageBoxManager.Yes = "Đồng ý!";
            MessageBoxManager.No = "Hủy";
            MessageBoxManager.Register();

            KiemTraThoiGian();
            LoadTraCuu();
        }
        private void KiemTraThoiGian()
        {
            ThamSoBUS thamSoBUS = new ThamSoBUS();
            int ngaycuoi = thamSoBUS.GetGiaTriThamSo(thamSoBUS.TG);
            int y = ngaycuoi / 10000;
            ngaycuoi = ngaycuoi % 10000;
            int m = ngaycuoi / 100;
            int d = ngaycuoi % 100;
            DateTime ngayCuoiDN = new DateTime(y, m, d);
            DateTime hientai = System.DateTime.Now;
            if (!hientai.Equals(ngayCuoiDN))
            {
                TimeSpan tgian = hientai - ngayCuoiDN;
                int soNgay = tgian.Days;
                for (int i = 0; i < soNgay; i++)
                {
                    UpdateTongNoTheoNgay();
                }
                string tempm = hientai.Month.ToString();
                if (hientai.Month < 10)
                    tempm = "0" + hientai.Month.ToString();
                string tempd =
                    hientai.Day.ToString();
                if (hientai.Day < 10) tempd = "0" + hientai.Day.ToString();
                int giatri = int.Parse(hientai.Year.ToString() + tempm + tempd);
                thamSoBUS.UpdateThamSo(thamSoBUS.TG, giatri);
            }
        }
        private void UpdateTongNoTheoNgay()
        {
            CuonSachBUS cuonsachBus = new CuonSachBUS();
            ThamSoBUS thamSoBUS = new ThamSoBUS();

            //Update Sách trễ hạn
            List<CUONSACH> ListCuonSachDangMuon = cuonsachBus.GetListCuonSachVoiTinhTrang(cuonsachBus.TT_DangMuon);
            foreach (CUONSACH cs in ListCuonSachDangMuon)
            {
                TimeSpan tgian = System.DateTime.Now - Convert.ToDateTime(cs.NgayMuon);

                //Xét ngày hôm qua
                if (tgian.Days - 1 > thamSoBUS.GetGiaTriThamSo(thamSoBUS.SoNgayMuonToiDa))
                    cuonsachBus.SetTinhTrangCuonSach(cs.MaCuonSach, cuonsachBus.TT_MuonQuaHan);
            }

            List<CUONSACH> ListCuonSachTreHan = cuonsachBus.GetListCuonSachVoiTinhTrang(cuonsachBus.TT_MuonQuaHan);
            DocGiaBUS docgiaBUS = new DocGiaBUS();
            foreach (CUONSACH cs2 in ListCuonSachTreHan)
            {
                DOCGIA docgia = docgiaBUS.GetDocGia(cs2.MaDocGia.Value);
                docgiaBUS.UpdateTongNo(cs2.MaDocGia.Value, thamSoBUS.GetGiaTriThamSo(thamSoBUS.SoTienPhat));
            }
        }

        #region Sach

        private void button_NhapSach_Click(object sender, EventArgs e)
        {
            Form frmNhapSach = new frmNhapSach(this);
            frmNhapSach.Show();
        }
        private void button_capNhatSach_Click(object sender, EventArgs e)
        {
            LoadDataGrid_DSSach();
        }

        private void button_xoaCuonSach_Click(object sender, EventArgs e)
        {
            frm_XoaCuonSach frmXoaSach = new frm_XoaCuonSach(this);
            frmXoaSach.Show();
        }
        private void button_xoaSach_Click(object sender, EventArgs e)
        {
            frm_XoaSach frmXoaSach = new frm_XoaSach(this);
            frmXoaSach.Show();
        }

        public void LoadDataGrid_DSSach()
        {
            int _STT_DSSach = 0;
            //clear 
            dataGridView_DSSach.RowCount = 1;
            dataGridView_DSSach.Rows.Clear();

            SachBUS sachBUS = new SachBUS();
            DauSachBUS dausachBUS = new DauSachBUS();
            TheLoaiSachBUS theloaiSachBUS = new TheLoaiSachBUS();
            List<SACH> listSACH = new List<SACH>();
            listSACH = sachBUS.GetListSach();

            foreach (SACH item in listSACH)
            {
                string tenTacGia = "";
                foreach (TACGIA item1 in sachBUS.GetListTacGia(item))
                {
                    tenTacGia += item1.TenTacGia + " , ";
                }
                _STT_DSSach++;
                dataGridView_DSSach.Rows.Add(_STT_DSSach, item.MaSach, sachBUS.GetDauSach(item).TenSach,
                    dausachBUS.GetTheLoaiSach(item.MaDauSach).TenTheLoaiSach, item.NhaXB, item.NamXB,
                   tenTacGia, item.SoLuong, item.GiaTien);
            }
        }

        #endregion

        #region Doc Gia
        private void button_themDG_Click(object sender, EventArgs e)
        {
            Form frmLapTheDocGia = new frmLapTheDG(this);
            frmLapTheDocGia.Show();
        }
        private void button_xoaDG_Click(object sender, EventArgs e)
        {
            frm_XoaDocGia formXoaDG = new frm_XoaDocGia(this);
            formXoaDG.Show();
        }
        private void button_capNhat_Click(object sender, EventArgs e)
        {
            LoadDataGrid_DSDocGia();
        }
        public void LoadDataGrid_DSDocGia()
        {
            int _STT_DSDocGia = 0;
            //clear 
            dataGridView_DSDocGia.RowCount = 1;
            dataGridView_DSDocGia.Rows.Clear();

            DocGiaBUS docgiaBUS = new DocGiaBUS();
            LoaiDocGiaBUS loaiDocGiaBUS = new LoaiDocGiaBUS();
            List<DOCGIA> listDOCGIA = new List<DOCGIA>();
            listDOCGIA = docgiaBUS.GetListDocGia();

            foreach (DOCGIA item in listDOCGIA)
            {
                _STT_DSDocGia++;
                dataGridView_DSDocGia.Rows.Add(_STT_DSDocGia, item.MaDG, item.Ten,
                   loaiDocGiaBUS.GetNameLoaiDocGiaByKey(item.MaLoaiDG), item.NgaySinh.ToShortDateString(),
                   item.DiaChi, item.Email, item.NgayLapThe.Value.ToShortDateString(), item.NgayHetHanThe.ToShortDateString());
            }
        }

        #endregion

        #region Quan he Sach - Doc Gia

        private void button_thuTienPhat_Click(object sender, EventArgs e)
        {
            Form frmThuTienPhat = new frmPhieuThuTienPhat();
            frmThuTienPhat.Show();
        }
        private void buttonCaiDat_Click(object sender, EventArgs e)
        {
            Form frmCaiDat = new frmCaiDat();
            frmCaiDat.Show();
        }
        private void button_muonSach_Click(object sender, EventArgs e)
        {
            Form frmMuonSach = new frmMuonSach(this);
            frmMuonSach.Show();
        }
        private void button_traSach_Click(object sender, EventArgs e)
        {
            Form frmTraSach = new frmPhieuTraSach();
            frmTraSach.Show();
        }

        #endregion

        #region Tra cuu

        private void LoadTraCuu()
        {
            CuonSachBUS cuonSachBUS = new CuonSachBUS();
            SachBUS sachBUS = new SachBUS();
            List<SACH> listSach = sachBUS.GetListSach();
            try
            {
                comboBox_toanTu.Items.Clear();
                List<string> toanTu = new List<string>() { "---", ">", "<", "=", "<=", ">=" };
                comboBox_toanTu.DataSource = toanTu;

                comboBox_tinhTrang.Items.Clear();
                List<string> listTinhTrang = new List<string>() { "---",cuonSachBUS.TT_TrongTV,
                                        cuonSachBUS.TT_DangMuon,cuonSachBUS.TT_MuonQuaHan};
                comboBox_tinhTrang.DataSource = listTinhTrang;

                comboBox_tenSach.Items.Clear();
                comboBox_tenSach.Items.Add("---");
                foreach (SACH s in listSach)
                {
                    if (!comboBox_tenSach.Items.Contains(sachBUS.GetDauSach(s).TenSach))
                        comboBox_tenSach.Items.Add(sachBUS.GetDauSach(s).TenSach);
                }

                comboBox_theLoai.Items.Clear();
                comboBox_theLoai.Items.Add("---");
                TheLoaiSachBUS theLoaiBUS = new TheLoaiSachBUS();
                List<string> listTheLoai = theLoaiBUS.GetListTheLoaiSach();
                comboBox_theLoai.Items.AddRange(listTheLoai.ToArray());

                comboBox_tacGia.Items.Clear();
                comboBox_tacGia.Items.Add("---");
                comboBox_tacGia.Items.AddRange(new TacGiaBUS().GetListTenTacGia().ToArray());

                comboBox_nhaXB.Items.Clear();
                comboBox_nhaXB.Items.Add("---");
                comboBox_nhaXB.Items.AddRange(sachBUS.GetListTenNXB().ToArray());
            }
            catch (Exception) { }
        }
        List<CUONSACH> listResult;
        private int LoadDataGridTraCuu()
        {
            dataGridView_dsTraCuu.RowCount = 1;
            dataGridView_dsTraCuu.Rows.Clear();
            TraCuu_KTDieuKien();
            if (listResult == null || listResult.Count == 0)
                return 0;

            int _stt = 0;
            SachBUS sachBUS = new SachBUS();
            foreach (CUONSACH cs in listResult)
            {
                _stt++;
                List<string> listTacGia = sachBUS.GetListTenTacGia(cs.MaSach);
                string tempTG = "";
                foreach (string item in listTacGia)
                {
                    tempTG += item + " , ";
                }

                dataGridView_dsTraCuu.Rows.Add(_stt, cs.MaCuonSach, sachBUS.GetDauSachByMaSach(cs.MaSach).TenSach,
                     sachBUS.GetTheLoaiSach(cs.MaSach).TenTheLoaiSach, tempTG, sachBUS.GetSach(cs.MaSach).NhaXB,
                        sachBUS.GetSach(cs.MaSach).NamXB, sachBUS.GetSach(cs.MaSach).GiaTien,
                        new CuonSachBUS().GetCuonSachByKey(cs.MaCuonSach).TinhTrang);
            }
            return 1;
        }
        private void TraCuu_KTDieuKien()
        {
            CuonSachBUS cuonSachBUS = new CuonSachBUS();
            listResult = cuonSachBUS.GetListCuonSach();
            List<CUONSACH> listIndex = new List<CUONSACH>();
            for (int i = 0; i < listResult.Count; i++)
            {
                SachBUS sachBUS = new SachBUS();
                CUONSACH cs = listResult[i];
                if (comboBox_tenSach.Text != "" && comboBox_tenSach.Text != "---")
                {
                    if (!sachBUS.GetDauSachByMaSach(cs.MaSach).TenSach.Equals(comboBox_tenSach.Text))// So sánh tên sách
                    {
                        if (!listIndex.Contains(cs))
                            listIndex.Add(cs);
                    }
                }

                if (comboBox_theLoai.Text != "" && comboBox_theLoai.Text != "---")
                {
                    if (!sachBUS.GetTheLoaiSach(cs.MaSach).TenTheLoaiSach.Equals(comboBox_theLoai.Text))
                    {
                        if (!listIndex.Contains(cs))
                            listIndex.Add(cs);
                    }
                }

                if (comboBox_tacGia.Text != "" && comboBox_tacGia.Text != "---")
                {
                    int MaTG = new TacGiaBUS().GetTacGia(comboBox_tacGia.Text).MaTacGia;
                    List<TACGIA> listtemp = sachBUS.GetListTacGia(cs.MaSach);
                    List<int> ListMaTG = new List<int>();
                    foreach (TACGIA item in listtemp)
                        ListMaTG.Add(item.MaTacGia);
                    if (!ListMaTG.Contains(MaTG))
                    {

                        if (!listIndex.Contains(cs))
                            listIndex.Add(cs);
                    }
                }

                if (comboBox_nhaXB.Text != "" && comboBox_nhaXB.Text != "---")
                {
                    if (!sachBUS.GetSach(cs.MaSach).NhaXB.Equals(comboBox_nhaXB.Text))
                    {
                        if (!listIndex.Contains(cs))
                            listIndex.Add(cs);
                    }
                }

                if (tb_namXB.Text != "")
                {
                    if (sachBUS.GetSach(cs.MaSach).NamXB != int.Parse(tb_namXB.Text))
                    {
                        if (!listIndex.Contains(cs))
                            listIndex.Add(cs);
                    }
                }

                if (comboBox_tinhTrang.Text != "" && comboBox_tinhTrang.Text != "---")
                {
                    if (!cuonSachBUS.GetCuonSachByKey(cs.MaCuonSach).TinhTrang.Equals(comboBox_tinhTrang.Text))
                    {
                        if (!listIndex.Contains(cs))
                            listIndex.Add(cs);
                    }
                }

                if (comboBox_toanTu.Text != "" && comboBox_toanTu.Text != "---" && tb_triGia.Text != "" && tb_triGia.Text != "0")
                {
                    int trigiaTraCuu = int.Parse(tb_triGia.Text);
                    int trigiaSach = sachBUS.GetSach(cs.MaSach).GiaTien;
                    switch (comboBox_toanTu.Text)
                    {
                        case ">":
                            if (trigiaSach <= trigiaTraCuu)
                            {
                                if (!listIndex.Contains(cs))
                                    listIndex.Add(cs);
                            }
                            break;
                        case "<":
                            if (trigiaSach >= trigiaTraCuu)
                            {
                                if (!listIndex.Contains(cs))
                                    listIndex.Add(cs);
                            }
                            break;
                        case "=":
                            if (trigiaSach != trigiaTraCuu)
                            {
                                if (!listIndex.Contains(cs))
                                    listIndex.Add(cs);
                            }
                            break;
                        case "<=":
                            if (trigiaSach > trigiaTraCuu)
                            {
                                if (!listIndex.Contains(cs))
                                    listIndex.Add(cs);
                            }
                            break;
                        case ">=":
                            if (trigiaSach < trigiaTraCuu)
                            {
                                if (!listIndex.Contains(cs))
                                    listIndex.Add(cs);
                            }
                            break;
                    }
                }
            }

            foreach (CUONSACH dele in listIndex)
                listResult.Remove(dele);
        }
        private void button_traCuu_Click(object sender, EventArgs e)
        {
            if (LoadDataGridTraCuu() == 0)
                MessageBox.Show("Không tìm thấy cuốn sách nào thỏa điều kiện!");
        }

        private void tb_triGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
            if (comboBox_toanTu.Text == "---")
            {
                MessageBox.Show("Vui lòng chọn phép so sánh!");
            }
        }
        private void tb_namXB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void comboBox_toanTu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void comboBox_namXB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        #endregion   

        #region Thống Kê
        private void button_XuatExcel1_Click(object sender, EventArgs e)
        {
            Export.Export2Excel(dataGridView_TKMuonSach);
        }
        private void button_XuatExcel2_Click(object sender, EventArgs e)
        {
            Export.Export2Excel(dataGridView_TKSachTre);
        }

        private void tab_traCuu_MouseDown(object sender, MouseEventArgs e)
        {
            LoadTraCuu();
        }
        private void LoadDataGridView_TKMuonSach()
        {
            dataGridView_TKMuonSach.RowCount = 1;
            dataGridView_TKMuonSach.Rows.Clear();
            int _stt = 0;
            try
            {
                int thang = int.Parse(tb_tk_MuonSach_thang.Text);
                int nam = int.Parse(tb_tk_MuonSach_nam.Text);

                THONGKEMUONSACH thongKe = new ThongKeMuonSachBUS().GetThongKeMuonSach(thang, nam);
                tb_tk_tongLuotMuon.Text = thongKe.TongLuotMuon.ToString();
                if (thongKe != null)
                {
                    List<THONGKEMUONSACHCT> listCTTK = new CTThongKeMuonSachBUS().GetCTThongKeMuonSach(thongKe.MaTKMS);
                    if (listCTTK != null)
                    {
                        foreach (THONGKEMUONSACHCT item in listCTTK)
                        {
                            _stt++;
                            dataGridView_TKMuonSach.Rows.Add(_stt, new TheLoaiSachBUS().GetTheLoaiByKey(item.MaTheLoaiSach).TenTheLoaiSach, item.LuotMuon, item.TiLe);
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Tháng hoặc năm sai!");
            }
        }
        private void LoadDataGridView_TKTraTre()
        {
            dataGridView_TKSachTre.RowCount = 1;
            dataGridView_TKSachTre.Rows.Clear();
            int _stt = 0;
            try
            {
                string NgayLap = dateTimePicker_tk_traTre.Value.ToString();
                CuonSachBUS cuonSachBUS = new CuonSachBUS();
                THONGKESACHTRATRE thongKe = new ThongKeSachTraTreBUS().GetThongKeTraTre(NgayLap);
                if (thongKe != null)
                {
                    List<THONGKETRATRECT> listCTTK = new CTThongKeSachTraTreBUS().GetCTThongKeTraTre(thongKe);
                    if (listCTTK != null)
                    {
                        foreach (THONGKETRATRECT item in listCTTK)
                        {
                            _stt++;
                            dataGridView_TKSachTre.Rows.Add(_stt, item.MaCuonSach, cuonSachBUS.GetTenSach(item.MaCuonSach), item.NgayMuon.ToShortDateString(), item.SoNgayTraTre);
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Ngày sai!");
            }
        }

        private void button_tk_muonSach_capNhat_Click(object sender, EventArgs e)
        {
            LoadDataGridView_TKMuonSach();
        }
        private void button_tk_traTre_capNhat_Click(object sender, EventArgs e)
        {
            LoadDataGridView_TKTraTre();
        }
        #endregion

    }
}
